package dao;

import entity.ColorSize;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;

import java.util.List;

/**
 * Created by jifeng on 2016/12/12 0012.
 */
public class ColorSizeDao {
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    private Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }
    public void addColorSize(ColorSize colorSize,int id){
        String sql="INSERT INTO colorSize(colorSizeName,subCategoryId) VALUE ('"+colorSize.getColorSizeName()+"',"+id+")";
        getCurrentSession().createSQLQuery(sql).executeUpdate();
    }
    public List<ColorSize> findColorSizeList(){
        List<ColorSize> colorSizeList=null;
        String sql ="select * from ColorSize";
        colorSizeList = getCurrentSession().createSQLQuery(sql).addEntity(ColorSize.class).list();
        return colorSizeList;
    }

    public  List<ColorSize> findColorSizeListById(int id){
        List<ColorSize> colorSizeList=null;
        String sql = "select * from ColorSize c where c.subCategoryId ="+id+"";
        colorSizeList = getCurrentSession().createSQLQuery(sql).addEntity(ColorSize.class).list();
        return colorSizeList;
    }

    public void deleteColorSize(int id){
       getCurrentSession().createQuery("delete from colorSize c where c.id ="+id+"").executeUpdate();
    }

    public void updateColorS(int id,String name){
        String sql="UPDATE colorSize c SET c.colorSizeName='"+name+"' WHERE c.id ="+id+"";
        getCurrentSession().createSQLQuery(sql).executeUpdate();
    }

}
